package com.stars.empoly.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.stars.empoly.R
import com.stars.empoly.activity.ReceivedDetailsActivity
import com.stars.empoly.adapter.HomeListAdapter
import com.stars.empoly.base.MBRFragment
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.databinding.FragmentHomeListBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.utils.PopuPayUtils
import com.stars.empoly.vmodel.HomeListVModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch

/**
 *on 2022/9/22
 */
class OrderListFragment : MBRFragment<HomeListVModel, HomeListAdapter, FragmentHomeListBinding>() {

    override fun __before(savedInstanceState: Bundle?) {
        super.__before(savedInstanceState)
        vm.status = arguments?.getString("status")

    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        binding.refreshLayout.isEnabled = false

        vm.type.observe(this) {

            PopuPayUtils.show(context(), vm.payType, vm.money, binding.ll, object : ChoiceData {
                override fun onRestuse(type: String?) {

                    vm.toPay(type)

                }

            })
        }

    }

    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)
        binding.refreshTo.isRefreshing = false

    }

    override fun listeners() {
        super.listeners()
        binding.recyclerView!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val topRowVerticalPosition =
                    if (recyclerView == null || recyclerView.childCount == 0) 0 else recyclerView.getChildAt(
                        0
                    ).top
                binding.refreshTo!!.isEnabled =
                    topRowVerticalPosition >= 0 && recyclerView != null && !recyclerView.canScrollVertically(
                        -1
                    )
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }
        })


        //设置刷新监听器
        binding.refreshTo.setColorSchemeResources(
            R.color.refresh_colorPrimary,
            R.color.refresh_colorAccent,
            R.color.refresh_colorPrimaryDark
        )
        binding.refreshTo.setOnRefreshListener {

            onRefresh()
        }
    }

    override fun vmClass(): Class<HomeListVModel> {
        return HomeListVModel::class.java
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeListBinding {
        return FragmentHomeListBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): HomeListAdapter {
        return HomeListAdapter(vm)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.PLACE_ORDER)])
    fun setPlaceOrder(type: String) {
        onRefresh()

    }

    @Subscribe(tags = [Tag(ConfigRx.PAY_COMPLETE)])
    fun setPayComplete(type: String) {


        onRefresh()

    }

    @Subscribe(tags = [Tag(ConfigRx.PAY_ERROR)])
    fun setPayError(type: String) {


//        toast("支付失败")

    }

}
